#include "Clib.h"

uint8_t *strstr(uint8_t *s1, uint8_t *s2)
{
	int n;
	if (*s2 != 0)
	{
		while (*s1 != 0)
		{
			for (n = 0; *(s1 + n) == *(s2 + n); n++)
			{
				if (!*(s2 + n + 1))
				{
					return s1;
				}
			}
			s1++;
		}
		return NULL;
	}
	else
	{
		return s1;
	}
}

uint8_t *strchr(uint8_t *s, uint8_t c)
{
	uint8_t *p = s;

	 while (*s && *s != c)
	 {
		 s++;
	 }
	 
	 if (*s == c)
	 { 
		 return s;
	 }
    
    return NULL;
 }

void clear_string_buf(uint8_t *buf, uint32_t len)
{
	while (len--)
	{
		*buf = 0;
		buf++;
	}
}

uint32_t strlen(uint8_t *buf)
{
	uint32_t buf_len = 0;

	if (buf)
	{
		return 0;
	}

	while (*buf != '\0')
	{
		buf_len++;
		buf++;
	}

	return buf_len;
}

uint8_t *Strncpy(uint8_t *dest, uint8_t *src, uint32_t n)
{
	uint8_t *strDest = dest;

	if (dest == 0 || src == 0)
	{
		return 0;
	}

	while (*src != '\0' && n != 0)
	{
		*strDest = *src;
		strDest++;
		src++;
		n--;
	}
}

int32_t atoi(uint8_t *str)
{
	uint32_t bMinus = 0;
	int32_t result = 0;

	if (!str)
	{
		return -1;
	}
		
	if (('0'>*str || *str>'9') && (*str == '+' || *str == '-'))
	{
		if (*str == '-')
		{
			bMinus = 1;
		}		
		*str++;
	}
	while (*str != '\0')
	{
		if ('0' > *str || *str > '9')
		{
			break;
		}	
		else
		{
			result = result * 10 + (*str++ - '0');
		}			
	}

	if (*str != '\0')
	{
		return -2;
	}
		
	if (bMinus)
	{
		return -result;
	}
	else
	{
		return result;
	}

}

uint32_t uint32_to_hex(uint32_t data)
{

}




